package com.gzhryc.shared_device.oem.code.agent.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.agent.dao.AgentRelationFuncModelDao;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentRelationFuncModel;
import com.gzhryc.shared_device.oem.code.agent.dao.models.RelationUserFuncModelCount;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AgentRelationFuncModelService {

    static Logger log = Logger.getLogger(AgentRelationFuncModelService.class);

    public static AgentRelationFuncModelService self() {
        return MultiDBTools.getService(AgentRelationFuncModelService.class, true);
    }

    public static AgentRelationFuncModelService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AgentRelationFuncModelService.class, true);
    }

    AgentRelationFuncModelDao dao;

    public AgentRelationFuncModelService(String jdbcKey) {
        this.dao = new AgentRelationFuncModelDao(jdbcKey);
    }

    public List<AgentRelationFuncModel> findJoinByUserKeys(Long userId,Long relationUserId){
        if(userId != null && relationUserId != null) {
            Conditions conditions = new Conditions(AgentRelationFuncModel.class);
            conditions.and().is("userId",userId);
            conditions.and().is("relationUserId", relationUserId);

            try {
                return dao.findJoinByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public AgentRelationFuncModel getJoinByKeys(Long userId,Long relationUserId, Long funcModelId){
        if(userId != null && relationUserId != null && funcModelId != null) {
            Conditions conditions = new Conditions(AgentRelationFuncModel.class);
            conditions.and().is("userId",userId);
            conditions.and().is("relationUserId", relationUserId);
            conditions.and().is("funcModelId",funcModelId);
            try {
                return dao.getJoin(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }
    
    public AgentRelationFuncModel getJoinByKeys(Long userId,Long relationUserId, String funcModelUrl){
        if(userId != null && relationUserId != null && StringTools.isNotBlank(funcModelUrl)) {
            Conditions conditions = new Conditions(AgentRelationFuncModel.class);
            conditions.and().is("userId",userId);
            conditions.and().is("relationUserId", relationUserId);
            conditions.and().is("funcModel.url",funcModelUrl);
            try {
                return dao.getJoin(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public List<RelationUserFuncModelCount> findFuncModelCountByUserKeys(Long userId, List<Long> relationUserIds){
        if(userId != null && relationUserIds != null && relationUserIds.size() > 0){
            Conditions conditions = new Conditions(AgentRelationFuncModel.class);
            conditions.and().is("userId",userId);
            conditions.and().in("relationUserId",relationUserIds);

            try {
                return dao.countGroupRelationUserId(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public boolean updateState(Long userId,Long relationUserId,Long funcModelId,Integer state){
        if(userId != null && funcModelId != null && state != null) {
            Conditions conditions = new Conditions(AgentRelationFuncModel.class);
            conditions.and().is("userId", userId);
            conditions.and().is("relationUserId", relationUserId);
            conditions.and().is("funcModelId", funcModelId);

            AgentRelationFuncModel updateEntity = new AgentRelationFuncModel();
            updateEntity.setState(state);

            try {
                return dao.updateNotNull(updateEntity, conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean add(AgentRelationFuncModel userRelationFuncModel) throws LogicException {
        AgentRelationFuncModel temp = getJoinByKeys(userRelationFuncModel.getUserId(),userRelationFuncModel.getRelationUserId()
                ,userRelationFuncModel.getFuncModelId());
        if(temp != null){
            throw new LogicException("功能模块已存在");
        }
        userRelationFuncModel.setCreateDate(new Date());
        try {
            return dao.insert(userRelationFuncModel) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean add(List<AgentRelationFuncModel> userRelationFuncModelList) throws LogicException {
        try {
            return dao.insert(userRelationFuncModelList) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean save(AgentRelationFuncModel agentRelationFuncModel) throws LogicException {
        AgentRelationFuncModel temp = getJoinByKeys(agentRelationFuncModel.getUserId(),agentRelationFuncModel.getRelationUserId(),agentRelationFuncModel.getFuncModelId());
        if(temp != null) {
            return true;
        }else{
            agentRelationFuncModel.setCreateDate(new Date());
            try {
                return dao.insert(agentRelationFuncModel) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByKeys(Long userId,Long relationUserId,Long funcModelId) throws LogicException{
        if(userId != null && relationUserId != null && funcModelId != null) {
            Conditions conditions = new Conditions(AgentRelationFuncModel.class);
            conditions.and().is("userId", userId);
            conditions.and().is("relationUserId", relationUserId);
            conditions.and().is("funcModelId", funcModelId);
            try {
                return dao.deleteByCondition(conditions) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByFuncModelId(Long funcModelId){
        if(funcModelId != null) {
            Conditions conditions = new Conditions(AgentRelationFuncModel.class);
            conditions.and().is("funcModelId", funcModelId);
            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteByUsers(Long userId,Long relationUserId) throws LogicException{
        if(userId != null && relationUserId != null) {
            Conditions conditions = new Conditions(AgentRelationFuncModel.class);
            conditions.and().is("userId", userId);
            conditions.and().is("relationUserId", relationUserId);

            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }
}
